﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities.Tracking;
using AppToolkit.WF.Utils;
using System.Collections.ObjectModel;

namespace AppToolkit.WF.Services
{
    public class WorkflowStateTrackingParticipant : TrackingParticipant
    {
        private Dictionary<Guid, WorkflowState> _Workflows = new Dictionary<Guid,WorkflowState>();

        protected override IAsyncResult BeginTrack(TrackingRecord record, TimeSpan timeout, AsyncCallback callback, object state)
        {
            this.Track(record, timeout);
            return new CompletedAsyncResult(callback, state);
        }

        protected override void EndTrack(IAsyncResult result)
        {
            CompletedAsyncResult.End(result);
        }

        protected override void Track(TrackingRecord record, TimeSpan timeout)
        {
                if (record is ActivityStateRecord)
                {
                    this.TrackActivityRecord((ActivityStateRecord)record);
                }
                else if (record is WorkflowInstanceRecord)
                {
                    this.TrackWorkflowRecord((WorkflowInstanceRecord)record);
                }
                //else if (record is BookmarkResumptionRecord)
                //{
                //    this.TrackBookmarkRecord((BookmarkResumptionRecord)record);
                //}
                //else if (record is ActivityScheduledRecord)
                //{
                //    this.TrackActivityScheduledRecord((ActivityScheduledRecord)record);
                //}
                //else if (record is CancelRequestedRecord)
                //{
                //    this.TrackCancelRequestedRecord((CancelRequestedRecord)record);
                //}
                //else if (record is FaultPropagationRecord)
                //{
                //    this.TrackFaultPropagationRecord((FaultPropagationRecord)record);
                //}
                else if (record is CustomTrackingRecord)
                {
                    this.TrackCustomRecord((CustomTrackingRecord)record);
                }
        }

        private void TrackCustomRecord(CustomTrackingRecord customTrackingRecord)
        {
            if (customTrackingRecord is ProgressTrackingRecord)
            {
                ProgressTrackingRecord ptr = (ProgressTrackingRecord)customTrackingRecord;

                WorkflowState state = ensureExistsWorkflowState(ptr.InstanceId);
                state.Current = ptr.Current;
                state.Maximum = ptr.Maximum;
            }
        }

        //private void TrackFaultPropagationRecord(FaultPropagationRecord faultPropagationRecord)
        //{
        //    throw new NotImplementedException();
        //}

        //private void TrackCancelRequestedRecord(CancelRequestedRecord cancelRequestedRecord)
        //{
        //    throw new NotImplementedException();
        //}

        //private void TrackActivityScheduledRecord(ActivityScheduledRecord activityScheduledRecord)
        //{
        //    throw new NotImplementedException();
        //}

        //private void TrackBookmarkRecord(BookmarkResumptionRecord bookmarkResumptionRecord)
        //{
        //    throw new NotImplementedException();
        //}

        private void TrackWorkflowRecord(WorkflowInstanceRecord workflowInstanceRecord)
        {
            WorkflowState state = ensureExistsWorkflowState(workflowInstanceRecord.InstanceId);
            state.State = workflowInstanceRecord.State;
        }

        private WorkflowState ensureExistsWorkflowState(Guid instanceID)
        {
            WorkflowState state;
            if (!this.Workflows.TryGetValue(instanceID, out state))
            {
                state = new WorkflowState(instanceID);
                this.Workflows[instanceID] = state;
            }
            return state;
        }

        private void TrackActivityRecord(ActivityStateRecord activityStateRecord)
        {
        }


        #region ViewModel API
        
        public Dictionary<Guid, WorkflowState> Workflows
        {
            get { return _Workflows; }
        }
        #endregion


//ActivityScheduledRecord
//ActivityStateRecord
//BookmarkResumptionRecord
//CancelRequestedRecord
//CustomTrackingRecord
//FaultPropagationRecord
//WorkflowInstanceRecord


    }
}